package com.tripwego.client.ui.widget;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.tripwego.client.model.DayModel;
import com.tripwego.client.ui.pageVoyage.IVoyageView;

public class DayHeaderWidget extends Composite implements IVoyageView, ClickHandler {

	private Presenter presenter;

	private EventBus eventBus;

	private DayModel dayModel;

	/*
	 * Elements graphiques
	 */
	private FlexTable viewPanel = new FlexTable();

	private Label lblTitle = new Label("title");

	private Label lblDate = new Label("date");
	private final HorizontalPanel horizontalPanel_actions = new HorizontalPanel();

	private Image image_delete = new Image("tripwego/sc/skins/Enterprise/images/headerIcons/close.png");
	private Image image_affecterEtapePrec = new Image("tripwego/sc/skins/Enterprise/images/headerIcons/arrow_up.png");
	private Image image_affecterEtapeSuiv = new Image("tripwego/sc/skins/Enterprise/images/headerIcons/arrow_down.png");

	/**
	 * 
	 */
	public DayHeaderWidget() {
		super();
		viewPanel.setStyleName("DayHeaderWidget");
		viewPanel.setSize("350px", "");

		lblTitle.setStyleName("lblTitleDay");

		viewPanel.setWidget(0, 0, lblTitle);
		viewPanel.getCellFormatter().setWidth(0, 0, "1%");
		viewPanel.getCellFormatter().setHeight(0, 0, "1%");
		lblTitle.setWidth("");
		lblTitle.setWordWrap(false);

		initWidget(viewPanel);
		lblDate.setWordWrap(false);
		lblDate.setStyleName("lblDateDay");

		viewPanel.setWidget(0, 1, lblDate);
		viewPanel.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_MIDDLE);
		viewPanel.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel_actions.setSpacing(2);
		horizontalPanel_actions.setStyleName("horizontalPanel_actions");

		viewPanel.setWidget(0, 2, horizontalPanel_actions);
		image_affecterEtapePrec.setTitle("Affecter le jour \u00E0 l'\u00E9tape pr\u00E9c\u00E9dente");

		horizontalPanel_actions.add(image_affecterEtapePrec);
		image_affecterEtapeSuiv.setTitle("Affecter le jour \u00E0 l'\u00E9tape suivante");

		horizontalPanel_actions.add(image_affecterEtapeSuiv);
		image_delete.setTitle("Supprimer le jour et d\u00E9caler les \u00E9tapes");
		horizontalPanel_actions.add(image_delete);
		viewPanel.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_RIGHT);
		viewPanel.getCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_MIDDLE);
		FlexTableHelper.fixRowSpan(viewPanel);

		viewPanel.addClickHandler(this);
		// image_delete.addClickHandler(this);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setEventBus(EventBus eventBus) {
		this.eventBus = eventBus;
	}

	@Override
	public void init() {
		if (dayModel != null) {
			lblTitle.setText(dayModel.getName());
			lblDate.setText(dayModel.getLibelleDate());

			if (!dayModel.isAttacheAEtape()) {
				horizontalPanel_actions.setVisible(true);

				viewPanel.setStyleDependentName("error", true);
				lblTitle.setStyleDependentName("error", true);
				lblDate.setStyleDependentName("error", true);
			} else {
				horizontalPanel_actions.setVisible(false);
			}
		}
	}

	@Override
	public void onClick(ClickEvent event) {
		// TODO Auto-generated method stub
	}

	/**
	 * @return the dayModel
	 */
	public DayModel getDayModel() {
		return dayModel;
	}

	/**
	 * @param dayModel
	 *            the dayModel to set
	 */
	public void setDayModel(DayModel dayModel) {
		this.dayModel = dayModel;
	}

}
